<template>
  <div class="container">
    <div class="select-container">
      <el-input
        size="small"
        style="width: 200px;"
        class="filter-item"
        placeholder="输入用户名称"
        clearable
        v-model="userName"
        prefix-icon="el-icon-search"
      >
      </el-input>
      <el-button size="small" type="success" @click="fetchData()" style="margin-right: 10px">查 询</el-button>
    </div>
    <div class="list-container">
      <div class="filter">
        <el-row :gutter="20">
          <el-col :span="10">
            <span style="line-height: 33px;padding-left:10px;">用户同步管理</span>
          </el-col>
          <el-col :span="14" style="text-align: right;">
            <el-button size="small" type="primary" @click="syncUsers()" style="margin-right: 10px">同步用户</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="data-contant">
        <div>
          <el-table
            :key='tableKey'
             @selection-change="handleSelectionChange"
            :data="list.records"
            v-loading="listLoading"
            element-loading-text="给我一点时间"
            size="small"
            stripe
            style="width: 100%;border:#eeeeee"
            highlight-current-row>
             <el-table-column
                type="selection"
                width="55">
             </el-table-column>
            <el-table-column
              align="center"
              type="index"
              width="80"
              label="编号"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="姓名"
              prop="name"
            >
            </el-table-column>

            <el-table-column
              align="center"
              label="登录名"
              prop="loginName"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="电子邮件"
              prop="email"
            >
            </el-table-column>
          
            

            <el-table-column
              align="center"
              label="状态" :formatter="fmtLength"
              prop="status"
            >
            </el-table-column>
            <el-table-column align="center" label="操 作" width="290px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                
                <el-button type="primary" size="small" @click="handleSyncUser(scope.row)">同步</el-button>
                
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              layout=" total, prev, pager, next"
              :page-size = "this.postList.rowBounds.limit"
              :total="this.list.count"
              @current-change="current_change"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import '@/styles/newstyle.scss'
import {
  selectNotSyncUsers,
  syncUser,
  syncSelectedUsers
} from '@/api/systemManage/userInfoExt'

export default {
  data() {
    return {
      
      tableKey: 0,
      userName:'',
      list: {},
      postList: {
        condition: [{
          'property': 'status',
          'value': '0',
          'type': 'EQ'
        }],
        sort: [{
          direction: 'DESC',
          property: 'lmTime'
        }],
        rowBounds: {
          offset: 0,
          limit: 10
        }
      },
      listLoading: false,
      temp: {
        'orgId': '',
        'loginName': '',
        'password': '',
        'name': '',
        
        'status': '',
        
        'email': ''
       
      },
      multipleSelection: []
      
     
    }
  },
  // 画面刚进入method
  created() {
    this.fetchData()
    
  },
  methods: {
     handleSelectionChange(val) {
     //  alert(JSON.stringify(val))
        this.multipleSelection = val;
        
      },
    fetchData() {

      this.listLoading = true
      /*
      var con = this.getUser()
       if(con.length==1){
         this.postList.condition.push(con[0])
       }
      */ 
     this.postList.condition.forEach(function(item, index, arr) {
          if(item.property == "name") {
              arr.splice(index, 1);
          }
        });
     if(this.userName!=''){
        this.postList.condition.push({
          'property': 'name',
          'value': this.userName,
          'type': 'LIKE'
        });
      }
      selectNotSyncUsers(this.postList).then(res => {
        this.list = res.data.data
        this.list.records = res.data.data.records
        this.listLoading = false
      }).catch((err) => {
        console.log(err)
      })
    },


    
 fmtLength(row, column) {
        const arr = row[column.property]
        /* 这里的 if(arr === undefined)
         * 是根据自己的需求，决定arr长度为0时的判断条件
         * 可以和这个不同
         */
        if (arr == '2') {
          return '已删除'
        } else if (arr == '0') {
          return '有效'
        }else if (arr == '1') {
          return '禁用'
        }
      },
    
    
    handleSyncUser(row) {
      

        this.temp.orgId=row.orgId
        this.temp.loginName=row.loginName
        this.temp.password=row.password
        this.temp.name=row.name
        
        this.temp.status=row.status
        
        this.temp.email=row.email
      syncUser(this.temp).then(res => {

       this.fetchData()

      }).catch((err) => {
        console.log(err)
      })
    },
    syncUsers() {
      if(this.multipleSelection.length==0)alert("请先选择要同步的人员")
      else{
         syncSelectedUsers(this.multipleSelection).then(res => {

           this.fetchData()

        }).catch((err) => {
          console.log(err)
        })
      }
    },
    
    current_change(currentPage) { // 通过分页获取数据
      const page = currentPage - 1
      this.postList.rowBounds.offset = page * 10
      this.fetchData()
    },
    
    /*
    getUser(){
      var flag=0
      var conditionEx = []
      for (let i = 0; i < this.$store.getters.roles.length; i++) {
          if (this.$store.getters.roles[i].code == "SYS_ADMIN") {
            //alert(this.$store.getters.roles[i].code)
            flag=1
            break
          }
      }
      if(flag==0){
        conditionEx.push({
          'property': 'addUser',
          'value': this.$store.getters.userid,
          'type': 'EQ'
        });
      }
      return conditionEx
    }
    */
  }
}
</script>

<style scoped>
  .container{
    color: #3b96f3;
    background: #fff;
    padding: 20px;
    height: 100%;
    overflow-y: auto;
  }
  .select-container{
    padding: 10px 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border: 1px solid #eeeeee;
  }
  .list-container{
    margin-top:30px;
    padding:0 10px 10px 10px;
    border:1px solid#eeeeee;
    border-radius: 4px;
  }
  .data-contant{
    background-color: rgba(0,0,0,.035);
    padding: 30px 10px 20px;
  }
  .data-detail{
    font-size: 14px;
    padding:20px 0;
    text-align: center;
  }
  .data-title{
    font-size: 14px;
    padding:10px 0;
    text-align: center;
  }
  .data-title div{
    font-size: 14px;
    padding:5px 0;
  }
  .filter{
    padding: 10px 5px;
    border-bottom: 2px solid#eeeeee;
  }
  .list-container{
  }
  .bold{
    font-weight: bold;
  }
</style>
